package com.gilight.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.dto.SysDictItemDto;
import com.gilight.base.model.SysDict;
import com.gilight.base.model.SysDictItem;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.ISysDictItemService;
import com.gilight.base.util.ShiroUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/web/sysDictItem")
public class SysDictItemController {

    @Resource
    ISysDictItemService sysDictItemService;

    @ApiOperation(value = "字典项列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "itemText", value = "字典项文本", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态（1启用 0不启用）", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "dictId", value = "字典id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/getSysDictItemList", method = RequestMethod.POST)
    public ResultEntity getSysDictItemList(@ApiIgnore @RequestBody SysDictItemDto sysDictItemDto) {
        ResultEntity resultEntity = new ResultEntity();
        PageInfo<SysDictItem> pageInfos = sysDictItemService.getSysDictItemList(sysDictItemDto);
        resultEntity.setData(pageInfos);
        return resultEntity;
    }

    @ApiOperation(value = "添加字典项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictId", value = "字典ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemText", value = "字典项文本", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemValue", value = "字典项值", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "状态（1启用 0不启用）", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultEntity add(@ApiIgnore @RequestBody SysDictItem sysDictItem) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        String userCode = sysUser.getUserCode();
        sysDictItem.setCreateBy(userCode);
        sysDictItem.setCreateTime(new Date());
        sysDictItem.setUpdateBy(userCode);
        sysDictItem.setUpdateTime(new Date());
        sysDictItemService.save(sysDictItem);
        return resultEntity;
    }

    @ApiOperation(value = "编辑字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典项ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemText", value = "字典项文本", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemValue", value = "字典项值", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "描述", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "状态（1启用 0不启用）", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultEntity edit(@ApiIgnore @RequestBody SysDictItem sysDictItem) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        String userCode = sysUser.getUserCode();
        sysDictItem.setUpdateBy(userCode);
        sysDictItem.setUpdateTime(new Date());
        sysDictItemService.updateById(sysDictItem);
        return resultEntity;
    }

    @ApiOperation(value = "根据ID删除字典项")
    @ApiImplicitParam(name = "id", value = "字典项ID", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultEntity delete(@RequestParam String id) {
        ResultEntity resultEntity = new ResultEntity();
        sysDictItemService.removeById(id);
        return resultEntity;
    }

    @ApiOperation(value = "根据字典项值和字典值获取相关的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemText", value = "字典项文本", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemValue", value = "字典项值", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictId", value = "字典Id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/getSysDictItemByCon", method = RequestMethod.POST)
    public ResultEntity getSysDictItemByCon(@RequestBody SysDictItem sysDictItem) {
        ResultEntity resultEntity = new ResultEntity();
        QueryWrapper<SysDictItem> query = new QueryWrapper<>(sysDictItem);
        List<SysDictItem> sysDictItemList = sysDictItemService.list(query);
        resultEntity.setData(sysDictItemList);
        return resultEntity;
    }
}
